package com.greendeliver.activities;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.test.R;
import com.greendeliver.business.EventManager;

public class EventTriviaDetailActivity extends Activity {

	private ArrayList<HashMap<String, String>>	eventsInfos;
	private String								ID_EVENT;

	private TextView							creationSpectacle;
	private TextView							nbActors;
	private TextView							rapportEvent;

	private ProgressDialog						pDialog;
	private Intent								i;

	private static final String					TAG_ID			= "ID_event";
	private static final String					TAG_CREATION	= "creation_date_event";
	private static final String					TAG_NBACTOR		= "number_actor_event";
	private static final String					TAG_LINK		= "linked_event";

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_event_trivia_detail );

		eventsInfos = new ArrayList<HashMap<String, String>>();
		this.pDialog = new ProgressDialog( EventTriviaDetailActivity.this );

		this.creationSpectacle = (TextView) findViewById( R.id.creationSpectacle );
		this.nbActors = (TextView) findViewById( R.id.nbActors );
		this.rapportEvent = (TextView) findViewById( R.id.rapportEvent );

		Intent i = getIntent();
		this.ID_EVENT = i.getStringExtra( TAG_ID );

		// Log.d( "id2", ID_EVENT );

		new Load().execute();
	}

	class Load extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute()
		{
			super.onPreExecute();
			Log.d( "Coucou", "1" );
			pDialog.setMessage( "Chargement des Informations" );
			pDialog.setIndeterminate( false );
			pDialog.setCancelable( true );
			pDialog.show();
			Log.d( "Coucou", "2" );
		}

		/**
		 * getting All products from url
		 * */
		@Override
		protected String doInBackground( String... args )
		{
			Log.d( "Coucou", "3" );
			EventManager em = new EventManager();
			Log.d( "Coucou", "4" );
			Log.d( ID_EVENT, "REGARDE" );
			eventsInfos = em.get_trivia_by_ID( ID_EVENT );
			Log.d( "Coucou", "5" );

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		@Override
		protected void onPostExecute( String file_url )
		{
			pDialog.dismiss();

			Log.d( "Coucou", "6" );

			Log.d( "Coucou", "7" );

			if ( eventsInfos.isEmpty() != true )
			{
				creationSpectacle.setText( eventsInfos.get( 0 ).get( TAG_CREATION ) );

				nbActors.setText( eventsInfos.get( 0 ).get( TAG_NBACTOR ) );

				rapportEvent.setText( eventsInfos.get( 0 ).get( TAG_LINK ) );
			}
		}
	}

}
